<?php
class xl_orders{
    function insert_order($data){
        global $dbh,$_POST;
        if($_POST['agree']==''){
        $sql='INSERT INTO orders (user_name,gender,email,phone,address,birthday,content,delivery_locations,payment_method,sum_money,code_active,date_added,date_modified,checkout)
                VALUE (:name,:gender,:email,:phone,:address,:birthday,:comment,:comment,:shipping_method,:sum_of_money,:code_active,:date_added,:date_modified,:checkout)';
        }else{
           $sql='INSERT INTO orders (user_name,gender,email,phone,address,birthday,content,delivery_locations,payment_method,sum_money,code_active,date_added,date_modified,checkout)
                VALUE (:name,:gender,:email,:phone,:address,:birthday,:comment,:address,:shipping_method,:sum_of_money,:code_active,:date_added,:date_modified,:checkout)';
        }
        $sth=$dbh->prepare($sql);
        $sth->execute($data);
        return $sth;
    }
    function check_name_order($name){
        global $dbh;
        $sql='SELECT id FROM orders WHERE user_name=:name ORDER BY id DESC LIMIT 0,1';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':name'=>$name));
        return $sth->fetch(PDO::FETCH_ASSOC);
    }
    function insert_order_product($data_order_product){
        global $dbh;
        $sql='INSERT INTO orders_detail (order_id,product_id,name,model,quantity,price,total) 
                VALUE(:id_order,:id_product,:name,:size,:quantity,:price,:into_money)';
        $sth=$dbh->prepare($sql);
        $sth->execute($data_order_product);
        return $sth;
    }
    function history_orders($email){
        global $dbh;
        $sql='SELECT orders.id,user_name,sum_money,date_added,checkout,COUNT(orders_detail.order_id) as count FROM orders,orders_detail WHERE orders.id=orders_detail.order_id AND email=:email GROUP BY orders.id';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':email'=>$email));
        return $sth->fetchAll();
    }
    function detail_history_orders($id){
        global $dbh;
        $sql='SELECT orders_detail.*,product.day_time,product.image,product.alias FROM orders,orders_detail,product WHERE product.id=orders_detail.product_id AND orders.id=orders_detail.order_id AND orders_detail.order_id=:id';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id));
        return $sth->fetchAll();
    }
    function order_user($email){
        global $dbh;
        $sql='SELECT * FROM orders WHERE email=:email ORDER BY id DESC LIMIT 0,1';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':email'=>$email));
        return $sth->fetch(PDO::FETCH_ASSOC);
    }
    function list_order_detail($id){
        global $dbh;
        $sql='SELECT detail.name,detail.model,detail.quantity,detail.price,detail.total,orders.date_added,product.image,product.day_time FROM orders,orders_detail AS detail,product WHERE detail.order_id = orders.id AND detail.product_id=product.id AND detail.order_id=:id';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id));
        return $sth->fetchAll();
    }
}